<div id="admin_user_list_panel">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>账号列表</legend>
    </fieldset>

    <blockquote class="layui-elem-quote">
        <div id="admin_user_mgr_ctrl_panel">
            <button class="layui-btn layui-btn-small">新增</button>
        </div>
    </blockquote>

    <div id="admin_user_search_panel">
        <div class="layui-input-inline">
            <input type="text" autocomplete="off" class="layui-input" placeholder="用户名,支持模糊查询">
        </div>
        <div class="layui-input-inline">
            <button class="layui-btn">搜索</button>
        </div>
    </div>

    <table class="layui-table">
        <thead>
        <tr>
            <th>账号</th>
            <th>密码</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>


        </tbody>
    </table>

    <div id="admin_user_pager"></div>
</div>

<div id="admin_user_edit_panel" style="display: none;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>新增/修改账号</legend>
    </fieldset>

    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-inline">
                <input type="text" name="account" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">菜单权限</label>
            <div class="layui-input-block">
                <ul id="admin_user_menu_tree" class="ztree" style="height: 200px;overflow-y: scroll;border-right: 1px solid #e6e6e6;border-left: 1px solid #e6e6e6;border-bottom: 1px solid #e6e6e6;">

                </ul>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="admin_user_edit_confirm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button class="layui-btn layui-btn-primary">返回</button>
            </div>
        </div>
    </form>
</div>

<script type="text/html" id="tpl_admin_user_list">
    {{if page_data.length>0}}
        {{each page_data as user_info i}}
        <tr>
            <td>{{user_info.account}}</td>
            <td>{{user_info.password}}</td>
            <td>{{user_info.create_time}}</td>
            <td><span class="admin-user-modify" data-i="{{i}}">修改</span>&nbsp;&nbsp;<span class="admin-user-delete" data-i="{{i}}">删除</span></td>
        </tr>
        {{/each}}
    {{else}}
    <tr>
        <td>暂无数据</td>
    </tr>
    {{/if}}
</script>

<script>
    layui.use(['laypage','form'], function(){
        var laypage = layui.laypage;
        var $ = layui.jquery;
        var layer = layui.layer;
        var ajaxUtil = layui.ajaxUtil;
        var form = layui.form();
        var commonUtil = layui.commonUtil;

        var ctrl = {};
        ctrl.admin_user_mgr_ctrl_panel = $('#admin_user_mgr_ctrl_panel');
        ctrl.admin_user_list_panel = $('#admin_user_list_panel');
        ctrl.admin_user_edit_panel = $('#admin_user_edit_panel');
        ctrl.admin_user_search_panel = $('#admin_user_search_panel');
        ctrl.admin_user_list_table = ctrl.admin_user_list_panel.find('tbody:eq(0)');

        var urls = {};
        urls.get_menu = './?_m=/admin/Menu/getMenu';
        urls.get_page_part = './?_m=/admin/page/getPart';
        urls.admin_user_save = './?_m=/admin/adminUser/save';
        urls.admin_user_query = './?_m=/admin/adminUser/query';
        urls.admin_user_delete = './?_m=/admin/adminUser/delete';

        var G = {};
        G.USERLIST_DATA = null;
        G.USERLIST_PAGE = 1;
        G.USERLIST_LIMIT = 10;

        var M = {};
        M.CURRENT_SELECTED = null;
        M.MODIFY_STATUS = false;

        var setting = {
            view: {
                selectedMulti: false
            },
            check: {
                enable: true,
                chkboxType: {"Y":"ps", "N":"ps"}
            },

            data: {
                simpleData: {
                    enable: true
                }
            }
        };

        function initTreeList(data) {
            var def = $.Deferred();

            var compareNodes = null;

            if(data) {
                if(!data.rights) {
                    data.rights = '[]';
                }

                compareNodes = JSON.parse(commonUtil.htmlDecode(data.rights));
            }

            ajaxUtil.doAjaxGet(urls.get_menu,null).done(function(ret) {
                if(ret.status == 0) {
                    var nodes = [];

                    var menus = ret.data.menus;
                    layui.each(menus,function(i,menuObj) {

                        var tempMain = {};
                        tempMain.name = menuObj.title;

                        tempMain.children = [];
                        layui.each(menuObj.nav_menu,function(i,navObj) {
                            var tempNav = {};
                            tempNav.name = navObj.title;
                            tempNav.children = [];

                            layui.each(navObj.third_menu,function(i,thirdObj) {
                                var tempThird = {};
                                tempThird.name = thirdObj.title;
                                tempThird.title = thirdObj.title;
                                tempThird.view = thirdObj.view;

                                if(data) {
                                    layui.each(compareNodes,function(i,compNode) {
                                        if(tempThird.name == compNode.title && tempThird.view == compNode.view && compNode.level == 2) {
                                            tempThird.checked = true;
                                        }
                                    });

                                    if(data.type == 0) {
                                        tempThird.checked = true;
                                        tempThird.chkDisabled  = true;
                                    }
                                }

                                tempNav.children.push(tempThird);
                            });

                            if(data) {
                                layui.each(compareNodes,function(i,compNode) {
                                    if(tempNav.name == compNode.title && tempNav.view == compNode.view && compNode.level == 1) {
                                        tempNav.checked = true;
                                    }
                                });

                                if(data.type == 0) {
                                    tempNav.checked = true;
                                    tempNav.chkDisabled  = true;
                                }
                            }

                            tempMain.children.push(tempNav);
                        });

                        if(data) {
                            layui.each(compareNodes,function(i,compNode) {
                                if(tempMain.name == compNode.title && tempMain.view == compNode.view && compNode.level == 0) {
                                    tempMain.checked = true;
                                }
                            });

                            if(data.type == 0) {
                                tempMain.checked = true;
                                tempMain.chkDisabled  = true;
                            }
                        }

                        nodes.push(tempMain);
                    });

                    $.fn.zTree.init($("#admin_user_menu_tree"), setting,nodes);
                }

                def.resolve();
            });

            return def.promise();
        }

        function showAdminUserEditPanel() {
            ctrl.admin_user_edit_panel.show();
            ctrl.admin_user_list_panel.hide();
        }

        function showAdminUserListPanel() {
            ctrl.admin_user_edit_panel.hide();
            ctrl.admin_user_list_panel.show();
        }

        function genAdminUserList(index) {
            var keyword = $.trim(ctrl.admin_user_search_panel.find('input:eq(0)').val());

            ajaxUtil.doAjaxGet(urls.admin_user_query,{'keyword':keyword,'page':index,'limit':G.USERLIST_LIMIT}).done(function(ret) {
                if(ret.status == 0) {
                    ctrl.admin_user_list_table.empty();

                    G.USERLIST_PAGE = index;
                    G.USERLIST_DATA = ret.data.page_data;

                    laypage({
                        cont: 'admin_user_pager',
                        pages: ret.data.last_page,
                        skin: '#1E9FFF',
                        curr:index,
                        jump: function(obj, first){
                            if(!first){
                                genAdminUserList(obj.curr);
                            }
                        }
                    });

                    ctrl.admin_user_list_table.append(template('tpl_admin_user_list',ret.data));
                }
            });
        }

        function bindEvent() {
            // 点击添加
            ctrl.admin_user_mgr_ctrl_panel.find('button:eq(0)').bind('click',function() {
                initTreeList();
                showAdminUserEditPanel();

                M.CURRENT_SELECTED = null;
                M.MODIFY_STATUS = false;

                $('#admin_user_edit_panel').find('button:eq(-2)').trigger('click');
            });

            //监听提交
            form.on('submit(admin_user_edit_confirm)', function(data){
                var args = {};
                args.account = data.field.account;
                args.password = data.field.password;
                var rights = [];

                var zTree = $.fn.zTree.getZTreeObj("admin_user_menu_tree");

                var checkedNodes = zTree.getCheckedNodes(true);

                layui.each(checkedNodes,function(i,nodesObj) {
//                    if(nodesObj.view !== undefined) {
                        var temp = {};
                        temp.title = nodesObj.name;
                        temp.view = nodesObj.view;
                        temp.level = nodesObj.level;

                        rights.push(temp);
//                    }
                });

                args.rights = JSON.stringify(rights);

                if(M.MODIFY_STATUS == true) {
                    args.id = M.CURRENT_SELECTED.id;
                }

                ajaxUtil.doAjaxPost(urls.admin_user_save,args).done(function(ret) {
                    if(ret.status == 0) {
                        $('#admin_user_edit_panel').find('button:eq(-1)').trigger('click');
                        showAdminUserListPanel();
                        genAdminUserList(1);
                    } else {
                        commonUtil.toast(ret.msg);
                    }
                });
                return false;
            });

            // 重置
            $('#admin_user_edit_panel').find('button:eq(-2)').bind('click',function() {
                initTreeList();
            });

            // 返回
            $('#admin_user_edit_panel').find('button:eq(-1)').bind('click',function() {
                showAdminUserListPanel();
                return false;
            });

            // 修改
            ctrl.admin_user_list_table.on({
                'click':function() {
                    var data = G.USERLIST_DATA[$(this).data('i')];

                    M.CURRENT_SELECTED = data;
                    M.MODIFY_STATUS = true;

                    showAdminUserEditPanel();
                    ctrl.admin_user_edit_panel.find('input:eq(0)').val(data.account);
                    ctrl.admin_user_edit_panel.find('input:eq(1)').val(data.password);
                    initTreeList(data);
                }
            },'.admin-user-modify');

            // 删除
            ctrl.admin_user_list_table.on({
                'click':function() {
                    var data = G.USERLIST_DATA[$(this).data('i')];

                    var confirmLayer = layer.confirm('确定删除？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){

                        if(data.type == 0) {
                            return commonUtil.toast('超级管理员账号不能删除');
                        }

                        layer.close(confirmLayer);
                        ajaxUtil.doAjaxGet(urls.admin_user_delete,{'id':data.id}).done(function(ret){
                            if(ret.status == 0) {
                                commonUtil.toast(ret.msg);
                                genAdminUserList(G.USERLIST_PAGE);
                            }
                        });
                    }, function(){

                    });
                }
            },'.admin-user-delete');
        }


        (function(){
            bindEvent();

            genAdminUserList(G.USERLIST_PAGE);
        })();
    });
</script>